package maven;

import java.io.File;

/**
 * 批量重命名
 *
 * @author ME
 * @date 2018年9月26日 下午4:43:00
 */
public class ChangeName {

	public static void main(String[] args) {
		// 文件名去广告
		ChangeName.reName1(new File("D:\\vedio\\111"),
				"【天时网 www.24s.net】");
	}

	/**
	 * 两位数转 01_ -> 001_
	 */
	public static void reName1(File file, String name) {
		String key = "_";
		File[] files = file.listFiles();
		for (int i = 0; i < files.length; i++) {
			File fileDetail = files[i];
			if (fileDetail.isFile()) {
				continue;
			} else {
				if (fileDetail.getName().contains(key)) {
					int i1 = fileDetail.getName().indexOf(key);
					if (i1 > 3) {
						System.out.println("重命名失败：" + fileDetail.getName());
						continue;
					}
					if (i1 == 3) {
						continue;
					}
					String n1 = fileDetail.getName().substring(0, i1);
					System.out.println("ReName:\t" + fileDetail.getName());
					File newFile2 = new File(fileDetail.getParent() + "/" + fileDetail.getName().replace(n1+key, String.format("%03d", Integer.parseInt(n1))+ key));
					fileDetail.renameTo(newFile2);
					System.out.println("TO:\t\t" + newFile2.getName());
					System.out.println("-----------------------------------------------------------------------------");
				} else {
					System.out.println("重命名失败：" + fileDetail.getName());
				}
			}
		}
	}

	public static void reName111(File file, String name) {
		String key = "_";
		File[] files = file.listFiles();
		for (int i = 0; i < files.length; i++) {
			File fileDetail = files[i];
			if (fileDetail.isFile()) {
				continue;
			} else {
				File[] list = fileDetail.listFiles();
				for (File file1 : list) {
					if (file1.isDirectory()) {
						if (file1.getName().equals("代码")) {
							if (file1.listFiles().length == 0) {
								file1.delete();
							}
						} else {
							File[] ll = file1.listFiles();
							for (File file2 : ll) {
								moveParent(file2);
							}
							file1.delete();
						}
					}
				}
			}
		}
	}

	private static void moveParent(File file) {
		File newF = new File(file.getParentFile().getParent() + "/" + file.getName());
		file.renameTo(newF);
	}

	public static void reName2(File file, String name) {
		File[] files = file.listFiles();
		for (int i = 0; i < files.length; i++) {
			File fileDetail = files[i];
			if (fileDetail.isDirectory()) {
				reName2(fileDetail, name);
				System.out.println("开始扫描文件夹:" + fileDetail.getAbsolutePath());
			} else {
				// if(fileDetail.getDisplayName().endsWith(".lastUpdated")){
				if (fileDetail.getName().contains(name)) {
					System.out.println("ReName:\t" + fileDetail.getName());
					File newFile2 = new File(fileDetail.getParent() + "/" + fileDetail.getName().replace(name, ""));
					fileDetail.renameTo(newFile2);
					System.out.println("TO:\t\t" + newFile2.getName());
					System.out.println("-----------------------------------------------------------------------------");
				} else {
					System.out.println("重命名失败：" + fileDetail.getName());
				}
			}
		}
	}

}
